package JDBC_learn;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo4 {
  @Test
    public void testResultSet() throws Exception{
      //1.注册驱动
      //  Class.forName("com.mysql.jdbc.Driver");
      //2.获取链接
      // String url="jdbc:mysql://127.0.0.1:3306/itheima";
      //如果连接的是本机的myaql并且端口号是默认的3306可以简化书写
      String url="jdbc:mysql:///itheima?useSSL=false";
      String username="root";
      String password="11235813";
      Connection conn = DriverManager.getConnection(url, username, password);
      //3.定义sql
      String sql="select*from account";
      //4.获取statement对象
      Statement stmt = conn.createStatement();
      //5.执行sql
      ResultSet rs = stmt.executeQuery(sql);
      //6.处理结果，遍历rs中的所有数据
//      while (rs.next()){
//          int id = rs.getInt(1);
//          String name=rs.getString(2);
//          double money=rs.getDouble(3);
//          System.out.println(id);
//          System.out.println(name);
//          System.out.println(money);
//          System.out.println("----------------");
//
//      }
      while (rs.next()){
          int id = rs.getInt("id");
          String name=rs.getString("name");
          double money=rs.getDouble("money");
          System.out.println(id);
          System.out.println(name);
          System.out.println(money);
          System.out.println("----------------");

      }
      rs.close();
      stmt.close();
      conn.close();
  }
}
